//
//  OrderViewController.m
//  Peso Guide
//
//  Created by macbook on 2025/6/5.
//

#import "OrderViewController.h"
#import "LoginViewController.h"
#import "OrderHeaderView.h"
#import "OrderViewCell.h"
#import "OrderEmptyCell.h"
#import "CustomWebViewController.h"
#import "MJRefresh/MJRefresh.h"
#import "ProductDetailViewController.h"
#import "MineSettingViewController.h"

@interface OrderViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) BaseTableView *tableView;
@property (nonatomic, strong) NSDictionary *dataDic;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) UIView *emptyView;
@property (nonatomic, strong) OrderHeaderView *headerView;
@property (nonatomic, strong) NSString *isn;

@end

@implementation OrderViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    if ([AppTools isLogin] == NO) {
        [AppTools toLogin];
        return;
    }
    
    self.isn = @"4";
    self.navView.backgroundColor = ColorHex(@"#1A7A35");
    self.backButton.hidden = YES;
    self.titleLabel.text = [Lang set:@"Order list"];
    self.backColor = ColorHex(@"#1A7A35");
    
    [self loadOrder];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.dataDic = [NSMutableDictionary dictionaryWithCapacity:0];
    self.dataArray = [NSMutableArray arrayWithCapacity:0];
    
    [self.view addSubview:self.tableView];
    
    WeakSelf;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf loadOrder];
    }];
}

- (void)loadOrder {
    
    [[AWHttpService service] postWithUrl:@"/trains/changed" params:@{@"isn":_isn} succ:^(id  _Nonnull responseObject) {
        if (SUCCESS) {
            self.dataDic = [responseObject safeObjectForKey:@"associating"];
            self.dataArray = [NSMutableArray arrayWithArray:[self.dataDic safeObjectForKey:@"below"]];
            
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
        }
    } fail:^(NSInteger errorType, id  _Nonnull error) {
        
    }];
}

- (void)chooseOrderType:(NSInteger)type {
    
    if (type == 0) {
        _isn = @"4";
    }
    if (type == 1) {
        _isn = @"7";
    }
    if (type == 2) {
        _isn = @"6";
    }
    if (type == 3) {
        _isn = @"5";
    }
    [self loadOrder];
}

#pragma mark - UITableViewDelegate&UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.dataDic.allKeys.count != 0 && self.dataArray.count == 0) {
        return 1;
    }
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.dataDic.allKeys.count != 0 && self.dataArray.count == 0) {
        NSString *OrderEmptyCellIdentifier = [NSString stringWithFormat:@"OrderEmptyCellIdentifier%ld",(long)indexPath.row];
        OrderEmptyCell *cell = [tableView dequeueReusableCellWithIdentifier:OrderEmptyCellIdentifier];
        if (!cell) {
            cell = [[OrderEmptyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:OrderEmptyCellIdentifier];
        }
        return cell;
    }

    NSString *OrderViewCellIdentifier = [NSString stringWithFormat:@"OrderViewCellIdentifier%ld",(long)indexPath.row];
    OrderViewCell *cell = [tableView dequeueReusableCellWithIdentifier:OrderViewCellIdentifier];
    if (!cell) {
        cell = [[OrderViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:OrderViewCellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (self.dataArray.count > 0) {
        cell.dataDic = [self.dataArray safeObjectAtIndex:indexPath.row];
    }
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.dataDic.allKeys.count != 0 && self.dataArray.count == 0) {
        return KScreenHeight-kStarBarHeight-kTabbarHeight-155;
    }
    NSMutableArray *performed = [NSMutableArray arrayWithArray:[[self.dataArray safeObjectAtIndex:indexPath.row] safeObjectForKey:@"performed"]];
    return 100+28*performed.count+15;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (_headerView == nil) {
        _headerView = [[OrderHeaderView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 155)];
    }
    _headerView.orderVC = self;
    return _headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 155;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *dataDic = [self.dataArray safeObjectAtIndex:indexPath.row];
    NSString *recent = [NSString stringWithFormat:@"%@",[dataDic safeObjectForKey:@"survives"]];

    if ([recent rangeOfString:@"http://"].location != NSNotFound ||
        [recent rangeOfString:@"https://"].location != NSNotFound) {
        //webView
        CustomWebViewController *webVC = [[CustomWebViewController alloc] init];
        webVC.linkUrl = recent;
        [[AppTools getCurrentVC].navigationController pushViewController:webVC animated:YES];
        return;
    }
    if ([recent rangeOfString:@"pe://so.gui.de/thelike"].location != NSNotFound) {
        //设置
        MineSettingViewController *settingVC = [[MineSettingViewController alloc] init];
        settingVC.hidesBottomBarWhenPushed = YES;
        [[AppTools getCurrentVC].navigationController pushViewController:settingVC animated:YES];
        return;
        return;
    }
    if ([recent rangeOfString:@"pe://so.gui.de/andlove"].location != NSNotFound) {
        //首页
        [AppTools getCurrentVC].tabBarController.selectedIndex = 0;
        return;
    }
    if ([recent rangeOfString:@"pe://so.gui.de/standard"].location != NSNotFound) {
        //登录
        [AppTools toLogin];
        return;
    }
    if ([recent rangeOfString:@"pe://so.gui.de/classes"].location != NSNotFound) {
        //订单
        [AppTools getCurrentVC].tabBarController.selectedIndex = 1;
        return;
    }
    if ([recent rangeOfString:@"pe://so.gui.de/islove"].location != NSNotFound) {
        //产品详情
        NSArray *array = [recent componentsSeparatedByString:@"="];
        NSString *theId = [NSString stringWithFormat:@"%@",[array safeObjectAtIndex:1]];
        ProductDetailViewController *vc = [[ProductDetailViewController alloc] init];
        vc.productId = theId;
        vc.hidesBottomBarWhenPushed = YES;
        [[AppTools getCurrentVC].navigationController pushViewController:vc animated:YES];
        return;
    }
}

#pragma mark - lazy load
- (BaseTableView *)tableView {
    if (!_tableView) {
        _tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, kStarBarHeight, KScreenWidth, KScreenHeight-kStarBarHeight-kTabbarHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = ColorHex(@"#FFFFFF");
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
